<template>
  <fix-area ref="fixArea">
    <div style="width: 100%; display: flex; justify-content: space-between; align-items: flex-start">
      <form-block ref="form" :model="showDetails">
        <el-form-item v-loading="loading" label="剧场名称" prop="name">
          <h3 style="margin: 0;padding: 0">{{ showDetails.name }}</h3>
        </el-form-item>
      </form-block>
      <el-button type="primary" @click="createVisible = true">添加</el-button>
    </div>
    <div style="flex: 1; min-height: 0; width: 100%">
      <table-block :loading="loading" :data="sceneList">
        <table-column label="排序" width="120px" prop="sort" fixed="left" />
        <table-column label="演出时间" prop="show_time" fixed="left" />
        <table-column label="状态" width="120px" prop="status" :use-slot="true">
          <template #default="{row}">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'" size="small">{{ row.status === 0 ? '显示' : '隐藏' }}</el-tag>
          </template>
        </table-column>
        <table-column label="操作" width="220px" :use-slot="true" fixed="right">
          <template #default="{row}">
            <el-button size="mini" type="warning" @click="editShowScene(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteShowScene(row.id)">删除</el-button>
            <el-button size="mini" type="info" @click="toShowSceneSeat(row.id)">座位</el-button>
          </template>
        </table-column>
      </table-block>
    </div>
    <pagination :current-page="form.page" :total="total" @current-change="currentChange" />
    <create v-if="createVisible" :visible="createVisible" :show-id="showId" @cancel="createVisible = false" @enter="createEnter" />
    <edit v-if="editVisible" :visible="editVisible" :show-scene-id="showSceneId" @cancel="editVisible = false" @enter="editEnter" />
  </fix-area>
</template>
<script>
import { showDetails, showSceneList, deleteShowScene } from '@/api/show'
import Create from '@/views/show/scene/component/Create.vue'
import Edit from '@/views/show/scene/component/Edit.vue'
export default {
  name: 'Scene',
  components: {
    Create, Edit
  },
  props: {
    showId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showDetails: {
        id: this.showId,
        name: ''
      },
      form: {
        show_id: this.showId,
        page: 1
      },
      total: 0,
      loading: false,
      createVisible: false,
      sceneList: [],
      editVisible: false,
      showSceneId: ''
    }
  },
  async mounted() {
    try {
      this.loading = true
      const showDetailsRes = await showDetails(this.showDetails)
      this.showDetails = showDetailsRes.data
      this.loading = false
      try {
        this.loading = true
        const sceneListRes = await showSceneList(this.form)
        this.sceneList = sceneListRes.data.data
        this.total = sceneListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    } catch (error) {
      this.$errorMessage()
      this.loading = false
    }
  },
  methods: {
    toShowSceneSeat(showSceneId) {
      this.$router.push({ name: 'Seat', query: { showSceneId }})
    },
    async deleteShowScene(showSceneId) {
      try {
        await this.$warningConfirm('确认删除?')
        try {
          this.loading = true
          await deleteShowScene({ show_scene_id: showSceneId })
          try {
            this.loading = true
            const sceneListRes = await showSceneList(this.form)
            this.sceneList = sceneListRes.data.data
            this.total = sceneListRes.data.total
            this.loading = false
          } catch (error) {
            this.$errorMessage()
            this.loading = false
          }
        } catch (error) {
          this.$errorMessage()
          this.loading = false
        }
      } catch (error) {
        console.log('取消删除')
      }
    },
    editShowScene(showSceneId) {
      this.showSceneId = showSceneId
      this.editVisible = true
    },
    async currentChange(event) {
      try {
        this.loading = true
        this.form.page = event
        const sceneListRes = await showSceneList(this.form)
        this.sceneList = sceneListRes.data.data
        this.total = sceneListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async editEnter() {
      this.editVisible = false
      try {
        this.loading = true
        const sceneListRes = await showSceneList(this.form)
        this.sceneList = sceneListRes.data.data
        this.total = sceneListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async createEnter() {
      this.createVisible = false
      try {
        this.loading = true
        const sceneListRes = await showSceneList(this.form)
        this.sceneList = sceneListRes.data.data
        this.total = sceneListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    }
  }
}
</script>
